home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / flex / flexs237.zoo / parse.y < prev    next >
Text File  |  1990-06-28  |  14KB  |  703 lines

  1.  
  2. /* parse.y - parser for flex input */
  3.  
  4. %token CHAR NUMBER SECTEND SCDECL XSCDECL WHITESPACE NAME PREVCCL EOF_OP
  5.  
  6. %{
  7. /*-
  8.  * Copyright (c) 1990 The Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * This code is derived from software contributed to Berkeley by
  12.  * Vern Paxson.
  13.  * 
  14.  * The United States Government has rights in this work pursuant
  15.  * to contract no. DE-AC03-76SF00098 between the United States
  16.  * Department of Energy and the University of California.
  17.  *
  18.  * Redistribution and use in source and binary forms are permitted provided
  19.  * that: (1) source distributions retain this entire copyright notice and
  20.  * comment, and (2) distributions including binaries display the following
  21.  * acknowledgement:  ``This product includes software developed by the
  22.  * University of California, Berkeley and its contributors'' in the
  23.  * documentation or other materials provided with the distribution and in
  24.  * all advertising materials mentioning features or use of this software.
  25.  * Neither the name of the University nor the names of its contributors may
  26.  * be used to endorse or promote products derived from this software without
  27.  * specific prior written permission.
  28.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  29.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  30.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  31.  */
  32.  
  33. #ifndef lint
  34. static char rcsid[] =
  35.     "@(#) $Header: /usr/fsys/odin/a/vern/flex/RCS/parse.y,v 2.7 90/06/27 23:48:31 vern Exp $ (LBL)";
  36. #endif
  37.  
  38. #include "flexdef.h"
  39.  
  40. int pat, scnum, eps, headcnt, trailcnt, anyccl, lastchar, i, actvp, rulelen;
  41. int trlcontxt, xcluflg, cclsorted, varlength, variable_trail_rule;
  42. Char clower();
  43.  
  44. static int madeany = false;  /* whether we've made the '.' character class */
  45. int previous_continued_action;    /* whether the previous rule's action was '|' */
  46.  
  47. %}
  48.  
  49. %%
  50. goal            :  initlex sect1 sect1end sect2 initforrule
  51.             { /* add default rule */
  52.             int def_rule;
  53.  
  54.             pat = cclinit();
  55.             cclnegate( pat );
  56.  
  57.             def_rule = mkstate( -pat );
  58.  
  59.             finish_rule( def_rule, false, 0, 0 );
  60.  
  61.             for ( i = 1; i <= lastsc; ++i )
  62.                 scset[i] = mkbranch( scset[i], def_rule );
  63.  
  64.             if ( spprdflt )
  65.                 fputs( "YY_FATAL_ERROR( \"flex scanner jammed\" )",
  66.                    temp_action_file );
  67.             else
  68.                 fputs( "ECHO", temp_action_file );
  69.  
  70.             fputs( ";\n\tYY_BREAK\n", temp_action_file );
  71.             }
  72.         ;
  73.  
  74. initlex         :
  75.             {
  76.             /* initialize for processing rules */
  77.  
  78.             /* create default DFA start condition */
  79.             scinstal( "INITIAL", false );
  80.             }
  81.         ;
  82.  
  83. sect1        :  sect1 startconddecl WHITESPACE namelist1 '\n'
  84.         |
  85.         |  error '\n'
  86.             { synerr( "unknown error processing section 1" ); }
  87.         ;
  88.  
  89. sect1end    :  SECTEND
  90.         ;
  91.  
  92. startconddecl   :  SCDECL
  93.             {
  94.             /* these productions are separate from the s1object
  95.              * rule because the semantics must be done before
  96.              * we parse the remainder of an s1object
  97.              */
  98.  
  99.             xcluflg = false;
  100.             }
  101.  
  102.         |  XSCDECL
  103.             { xcluflg = true; }
  104.         ;
  105.  
  106. namelist1    :  namelist1 WHITESPACE NAME
  107.             { scinstal( nmstr, xcluflg ); }
  108.  
  109.         |  NAME
  110.             { scinstal( nmstr, xcluflg ); }
  111.  
  112.         |  error
  113.                         { synerr( "bad start condition list" ); }
  114.         ;
  115.  
  116. sect2           :  sect2 initforrule flexrule '\n'
  117.         |
  118.         ;
  119.  
  120. initforrule     :
  121.             {
  122.             /* initialize for a parse of one rule */
  123.             trlcontxt = variable_trail_rule = varlength = false;
  124.             trailcnt = headcnt = rulelen = 0;
  125.             current_state_type = STATE_NORMAL;
  126.             previous_continued_action = continued_action;
  127.             new_rule();
  128.             }
  129.         ;
  130.  
  131. flexrule        :  scon '^' rule
  132.                         {
  133.             pat = $3;
  134.             finish_rule( pat, variable_trail_rule,
  135.                      headcnt, trailcnt );
  136.  
  137.             for ( i = 1; i <= actvp; ++i )
  138.                 scbol[actvsc[i]] =
  139.                 mkbranch( scbol[actvsc[i]], pat );
  140.  
  141.             if ( ! bol_needed )
  142.                 {
  143.                 bol_needed = true;
  144.  
  145.                 if ( performance_report )
  146.                 pinpoint_message( 
  147.                 "'^' operator results in sub-optimal performance" );
  148.                 }
  149.             }
  150.  
  151.         |  scon rule
  152.                         {
  153.             pat = $2;
  154.             finish_rule( pat, variable_trail_rule,
  155.                      headcnt, trailcnt );
  156.  
  157.             for ( i = 1; i <= actvp; ++i )
  158.                 scset[actvsc[i]] =
  159.                 mkbranch( scset[actvsc[i]], pat );
  160.             }
  161.  
  162.                 |  '^' rule
  163.             {
  164.             pat = $2;
  165.             finish_rule( pat, variable_trail_rule,
  166.                      headcnt, trailcnt );
  167.  
  168.             /* add to all non-exclusive start conditions,
  169.              * including the default (0) start condition
  170.              */
  171.  
  172.             for ( i = 1; i <= lastsc; ++i )
  173.                 if ( ! scxclu[i] )
  174.                 scbol[i] = mkbranch( scbol[i], pat );
  175.  
  176.             if ( ! bol_needed )
  177.                 {
  178.                 bol_needed = true;
  179.  
  180.                 if ( performance_report )
  181.                 pinpoint_message(
  182.                 "'^' operator results in sub-optimal performance" );
  183.                 }
  184.             }
  185.  
  186.                 |  rule
  187.             {
  188.             pat = $1;
  189.             finish_rule( pat, variable_trail_rule,
  190.                      headcnt, trailcnt );
  191.  
  192.             for ( i = 1; i <= lastsc; ++i )
  193.                 if ( ! scxclu[i] )
  194.                 scset[i] = mkbranch( scset[i], pat );
  195.             }
  196.  
  197.                 |  scon EOF_OP
  198.             { build_eof_action(); }
  199.  
  200.                 |  EOF_OP
  201.             {
  202.             /* this EOF applies to all start conditions
  203.              * which don't already have EOF actions
  204.              */
  205.             actvp = 0;
  206.  
  207.             for ( i = 1; i <= lastsc; ++i )
  208.                 if ( ! sceof[i] )
  209.                 actvsc[++actvp] = i;
  210.  
  211.             if ( actvp == 0 )
  212.                 pinpoint_message(
  213.         "warning - all start conditions already have <<EOF>> rules" );
  214.  
  215.             else
  216.                 build_eof_action();
  217.             }
  218.  
  219.                 |  error
  220.             { synerr( "unrecognized rule" ); }
  221.         ;
  222.  
  223. scon            :  '<' namelist2 '>'
  224.         ;
  225.  
  226. namelist2       :  namelist2 ',' NAME
  227.                         {
  228.             if ( (scnum = sclookup( nmstr )) == 0 )
  229.                 format_pinpoint_message(
  230.                 "undeclared start condition %s", nmstr );
  231.  
  232.             else
  233.                 actvsc[++actvp] = scnum;
  234.             }
  235.  
  236.         |  NAME
  237.             {
  238.             if ( (scnum = sclookup( nmstr )) == 0 )
  239.                 format_pinpoint_message(
  240.                 "undeclared start condition %s", nmstr );
  241.             else
  242.                 actvsc[actvp = 1] = scnum;
  243.             }
  244.  
  245.         |  error
  246.             { synerr( "bad start condition list" ); }
  247.         ;
  248.  
  249. rule            :  re2 re
  250.             {
  251.             if ( transchar[lastst[$2]] != SYM_EPSILON )
  252.                 /* provide final transition \now/ so it
  253.                  * will be marked as a trailing context
  254.                  * state
  255.                  */
  256.                 $2 = link_machines( $2, mkstate( SYM_EPSILON ) );
  257.  
  258.             mark_beginning_as_normal( $2 );
  259.             current_state_type = STATE_NORMAL;
  260.  
  261.             if ( previous_continued_action )
  262.                 {
  263.                 /* we need to treat this as variable trailing
  264.                  * context so that the backup does not happen
  265.                  * in the action but before the action switch
  266.                  * statement.  If the backup happens in the
  267.                  * action, then the rules "falling into" this
  268.                  * one's action will *also* do the backup,
  269.                  * erroneously.
  270.                  */
  271.                 if ( ! varlength || headcnt != 0 )
  272.                 {
  273.                 fprintf( stderr,
  274.     "%s: warning - trailing context rule at line %d made variable because\n",
  275.                      program_name, linenum );
  276.                 fprintf( stderr,
  277.                      "      of preceding '|' action\n" );
  278.                 }
  279.  
  280.                 /* mark as variable */
  281.                 varlength = true;
  282.                 headcnt = 0;
  283.                 }
  284.  
  285.             if ( varlength && headcnt == 0 )
  286.                 { /* variable trailing context rule */
  287.                 /* mark the first part of the rule as the accepting
  288.                  * "head" part of a trailing context rule
  289.                  */
  290.                 /* by the way, we didn't do this at the beginning
  291.                  * of this production because back then
  292.                  * current_state_type was set up for a trail
  293.                  * rule, and add_accept() can create a new
  294.                  * state ...
  295.                  */
  296.                 add_accept( $1, num_rules | YY_TRAILING_HEAD_MASK );
  297.                 variable_trail_rule = true;
  298.                 }
  299.             
  300.             else
  301.                 trailcnt = rulelen;
  302.  
  303.             $$ = link_machines( $1, $2 );
  304.             }
  305.  
  306.         |  re2 re '$'
  307.             { synerr( "trailing context used twice" ); }
  308.  
  309.         |  re '$'
  310.                         {
  311.             if ( trlcontxt )
  312.                 {
  313.                 synerr( "trailing context used twice" );
  314.                 $$ = mkstate( SYM_EPSILON );
  315.                 }
  316.  
  317.             else if ( previous_continued_action )
  318.                 {
  319.                 /* see the comment in the rule for "re2 re"
  320.                  * above
  321.                  */
  322.                 if ( ! varlength || headcnt != 0 )
  323.                 {
  324.                 fprintf( stderr,
  325.     "%s: warning - trailing context rule at line %d made variable because\n",
  326.                      program_name, linenum );
  327.                 fprintf( stderr,
  328.